class User < ActiveRecord::Base
  connection.execute "SET NAMES UTF8"
  #before_create :encrypt_password

  has_many :chats
  has_many :logs
  has_many :activities
  has_many :exam_results
  has_many :exams, :through => :exam_results
  has_one :blog
  has_many :posts, :order=>'created_at DESC'
  belongs_to :lao
  file_column :image

  validates_uniqueness_of :login, :message=>'รหัสผู้ใช้งานนี้ถูกใช้โดยผู้อื่นแล้ว'
  #validates_uniqueness_of :email, :message=>'อีเมล์นี้ถูกใช้โดยผู้อื่นแล้ว'
  validates_presence_of :login, :message=>'โปรดกำหนดรหัสผู้ใช้'
  validates_presence_of :password, :message=>'โปรดกำหนดรหัสผ่าน'
#  validates_confirmation_of :password, :message=>'รหัสผ่านและรหัสตรวจสอบไม่ตรงกัน'
  validates_presence_of :fname, :lname, :message=>'โปรดบันทึกชื่อและนามสกุล'
  validates_uniqueness_of :nid, :message=>'หมายเลขนี้ถูกใช้โดยผู้อื่นแล้ว', :on=>:create
  validates_presence_of :nid, :message=>'โปรดบันทึก', :on=>:create
#  validates_format_of :nid, :with =>/\d{13}/,:message=>'เลขประจำตัวประชาชนไม่ถูกต้อง', :on=>:create
#  validates_length_of :nid, :is =>13,:message=>'เลขประจำตัวประชาชนไม่ถูกต้อง', :on=>:create
  validates_presence_of :email, :message=>'โปรดบันทึก email', :on=>:create
  validates_format_of :email, :with =>/^([0-9a-zA-Z+_]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/,
    :message=>'รหัสไปรษณีย์อิเล็กทรอนิกส์ไม่ถูกต้อง ห้ามใช้เครื่องหมายพิเศษใน Email', :on=>:create
#  validates_presence_of :tambon, :message=>'โปรดบันทึกตำบล', :on=>:create
#  validates_presence_of :amphoe, :message=>'โปรดบันทึกอำเภอ', :on=>:create
  validates_presence_of :province, :message=>'โปรดบันทึกจังหวัด', :on=>:create
  validates_format_of :zip, :with =>/\d{5}/,:message=>'รหัสไปรษณีย์ไม่ถูกต้อง',
    :if=>Proc.new {|u| !u.zip.blank?}
  validates_format_of :phone, :with =>/0\d{8}/,:message=>'0xxxxxxxx',
    :if=>Proc.new {|u| !u.phone.blank?}
  validates_format_of :cellphone, :with =>/08\d{8}/,:message=>'08xxxxxxxx',
    :if=>Proc.new {|u| !u.cellphone.blank?}

  attr_accessor :password_confirmation

#  named_scope :graduated, :conditions=>'graduation_on IS NOT NULL', :order=>'graduation_on'

  def full_name
    "#{title}#{fname} #{lname}"
  end
  def e_full_name
    "#{e_title}#{e_fname} #{e_lname}"
  end
  def self.authenticate(login,password)
    h= Digest::SHA1.hexdigest(password)
    User.find_by_login_and_password(login,h)
  end
#  def lao_name
#    lao.th if lao
#  end
#  def lao_name=(name)
#    self.lao= Lao.find_by_th(name)
#  end
#  def set_lao
#    return unless lao_code.match(/\d{6}/)
#    lao= Lao.find_by_code lao_code
#    self.lao_id= lao.id
#  end
  def encrypt_password
    pub_key =  Crypto::Key.from_file('turac_key.pub')
    self.epassword= pub_key.encrypt(self.password)
    self.password= Digest::SHA1.hexdigest(self.password)
  end
  def has_blog?
    blog!=nil
  end

#  def blog
#    blogs.first
#  end
  def chat_session
    current_time= Time.now.utc
    chat_session= ChatSession.first :conditions=>["start <= ? AND stop >= ?",current_time, current_time]
    return false unless chat_session
    chat_session.chat_participants.each do |u|
      return true if u.user_id == self.id
    end
    return false
  end
  def pass_courses
#    @exam_results= ExamResult.find_all_by_user_id self.id
    exams = []
    self.exam_results.each do |r|
      next unless r.exam
      next if r.percent < r.exam.score_c
      exams << r.course_id unless exams.include? r.course_id
    end
    exams.size
  end
  def certified?
    if graduation_on
      return true
    elsif pass_courses >= 5
      update_attribute :graduation_on, Date.today
      return true
    else
      return false
    end
  end
  def self.count_cert
    c= 0
    find_each(:batch_size => 300, :include=>:exam_results) do |u|
      c += 1 if u.certified?
    end
    return c
  end
  # find all certified users
  def self.certified
    uu=[]
    find_each(:batch_size => 300, :include=>:exam_results) do |u|
      uu << u if u.certified?
    end
    return uu
  end
  def self.certified53
    find [78, 121, 129, 169, 177, 214, 219, 300, 321, 342, 344, 346, 347, 348, 352, 368, 384, 386, 395, 400, 401, 417, 423, 424, 428, 445, 446, 449, 451, 454, 456, 474, 479, 502, 503, 524, 540, 541, 542, 545, 546, 547, 550, 553, 566, 582, 585, 593, 600, 601, 602, 603, 615, 621, 627, 631, 632, 654, 663, 679, 681, 693, 708, 711, 718, 721, 723, 724, 725, 729, 731, 749, 755, 764, 765, 770, 782, 796, 799, 805, 806, 807, 815, 821, 855, 858, 865, 869, 871, 879, 885, 887, 893, 909, 928, 930, 932, 933, 938, 944, 950, 955, 958, 963, 969, 984, 995, 1011, 1016, 1019, 1021, 1028, 1038, 1050, 1060, 1062, 1063, 1064, 1065, 1067, 1068, 1069, 1078, 1084, 1085, 1088, 1089, 1105, 1109, 1111, 1235, 1253, 1263, 1264, 1265, 1295, 1303, 1324, 1327, 1330, 1347, 1373, 1374, 1375, 1376, 1378, 1391, 1397, 1398, 1403, 1409, 1410, 1411, 1412, 1413, 1434, 1439, 1441, 1442, 1456, 1457, 1464, 1466, 1477, 1482, 1484, 1485, 1486, 1488, 1490, 1491, 1493, 1495, 1496, 1498, 1500, 1501, 1502, 1505, 1507, 1509, 1514, 1515, 1517, 1518, 1537, 1558, 1560, 1566, 1568, 1569, 1583, 1584, 1590, 1630, 1645, 1656, 1662, 1691, 1694, 1695, 1705, 1714, 1722, 1723, 1724, 1765, 1770, 1793, 1795, 1806, 1812, 1837, 1838, 1850, 1854, 1856, 1873, 1874, 1876, 1878, 1880, 1882, 1883, 1892, 1894, 1897, 1918, 1927, 1930, 1932, 1933, 1934, 1936, 1937, 1939, 1942, 1943, 1944, 1945, 1946, 1947, 1950, 1952, 1954, 1955, 1957, 1958, 1960, 1964, 1966, 1967, 1969, 1970, 1973, 1976, 1980, 1982, 1986, 1988, 1993, 1994, 1995, 1997, 1998, 2008, 2013, 2016, 2021, 2025, 2027, 2032, 2035, 2036, 2037, 2041, 2042, 2046, 2047, 2048, 2055, 2097, 2099, 2103, 2104, 2105, 2109, 2113, 2119, 2126, 2127, 2129, 2131, 2132, 2133, 2134, 2136, 2142, 2158, 2163, 2164, 2167, 2202, 2204, 2211, 2212, 2221, 2224, 2227, 2228, 2229, 2232, 2233, 2234, 2235, 2236, 2237, 2238, 2242, 2246, 2248, 2249, 2269, 2284, 2285, 2288, 2290, 2294, 2295, 2296, 2297, 2299, 2300, 2305, 2307, 2310, 2311, 2315, 2316, 2317, 2318, 2323, 2328, 2331, 2332, 2333, 2335, 2336, 2337, 2338, 2339, 2341, 2342, 2344, 2346, 2352, 2355, 2358, 2360, 2366, 2374, 2375, 2383, 2385, 2387, 2397, 2399, 2419, 2421, 2423, 2429, 2430, 2432, 2436, 2443, 2444, 2445, 2447, 2452, 2468, 2469, 2471, 2472, 2475, 2477, 2478, 2479, 2480, 2481, 2483, 2484, 2487, 2488, 2489, 2490, 2491, 2493, 2496, 2497, 2498, 2500, 2501, 2514, 2521, 2535, 2538, 2540, 2544, 2550, 2551, 2555, 2556, 2558, 2561, 2563, 2565, 2569, 2576, 2581, 2582, 2583, 2584, 2591, 2592, 2593, 2594, 2597, 2605, 2608, 2609, 2611, 2614, 2617, 2619, 2622, 2623, 2624, 2625, 2626, 2627, 2629, 2637, 2642, 2643, 2647, 2659, 2664, 2667, 2668, 2669, 2670, 2671, 2673, 2675, 2678, 2679, 2682, 2683, 2684, 2685, 2686, 2690, 2691, 2699, 2704, 2707, 2719, 2720, 2730, 2731, 2732, 2733, 2735, 2737, 2741, 2746, 2747, 2748, 2749, 2750, 2752, 2763, 2764, 2770, 2772, 2776, 2777, 2778, 2784, 2785, 2788, 2791, 2803, 2807, 2808, 2809, 2812, 2815, 2819, 2820, 2822, 2823, 2837, 2838, 2839, 2842, 2844, 2845, 2846, 2847, 2848, 2850, 2853, 2856, 2860, 2862, 2867, 2870, 2873, 2874, 2879, 2887, 2888, 2890, 2891, 2893, 2895, 2896, 2903, 2904, 2906, 2908, 2910, 2912, 2913, 2914, 2916, 2917, 2918, 2926, 2928, 2932, 2933, 2935, 2936, 2938, 2940, 2941, 2943, 2947, 2949, 2950, 2951, 2952, 2953, 2954, 2955, 2956, 2958, 2964, 2966, 2967, 2968, 2974, 2977, 2979, 2980, 2987, 2989, 2995, 2998, 3000, 3004, 3005, 3007, 3015, 3016, 3017, 3018, 3019, 3020, 3021, 3022, 3023, 3024, 3025, 3027, 3028, 3029, 3030, 3031, 3032, 3033, 3034, 3037, 3038, 3040, 3041, 3042, 3044, 3045, 3047, 3048, 3049, 3050, 3051, 3052, 3054, 3056, 3059, 3060, 3068, 3069, 3070, 3077, 3079, 3085, 3088, 3095, 3116, 3122, 3133, 3134, 3136, 3139, 3140, 3141, 3143, 3146, 3148, 3149, 3150, 3151, 3155, 3158, 3161, 3162, 3163, 3164, 3166, 3167, 3169, 3172, 3173, 3174, 3175, 3177, 3181, 3182, 3185, 3186, 3187, 3195, 3199, 3220, 3232, 3234, 3235]
#    find [78, 121, 129, 3234, 3235]
  end

#  def tambon=(tambon_id)
#    if tambon_id=~/\d+/
#      begin
#        tambon= Tambon.find_by_code tambon_id
#        self.tambon= tambon.name
#      rescue
#      end
#    end
#  end
#  def amphoe=(amphoe_id)
#    if amphoe_id=~/\d+/
#      begin
#        amphoe= Amphoe.find_by_code amphoe_id
#        self.amphoe= amphoe.th
#      rescue
#      end
#    end
#  end
#  def province=(province_id)
#    if province_id=~/\d+/
#      begin
#        province= Province.find_by_code tambon_id
#        self.province= province.th
#      rescue
#      end
#    end
#  end
end

